<?php require_once(dirname(__FILE__)."/config/config.php"); ?>
  <?php include (dirname(__FILE__)."/include/header.php"); ?>
<?php
//保存购物车数据进数据库.并清空购物车
if($_REQUEST['method']=='saveCart'){
	$totalPrice=totalPrice();
	$add_id=$_REQUEST['add_id'];
	$remark=$_REQUEST['remark'];
	$tel=$_REQUEST['tel'];
	$phone=$_REQUEST['phone'];
	$address=$_REQUEST['address'];
	$shop_id=$_SESSION['_cart']['shop_id'];
	if(isset($_SESSION['_user'])){
		$user_id=$_SESSION['_user']['user_id'];
		$user_name=$_SESSION['_user']['username'];
		//判断用户是登录用户,已登录状态
		if($add_id==""){
			//add_id为空时,为新地址.
		}
	}else{
		//判断用户是游客,未登录状态
		$user_id=0;
		$user_name="游客";
	}
	$sql="INSERT INTO `zy_order`(`shop_id`,`user_id`,`add_id`,`totalPrice`,`order_time`,`order_status`,`remark`,`is_delete`,`create_user`,`create_time`,`update_user`,`update_time`) VALUES ('$shop_id','$user_id','$add_id','$totalPrice',NOW(),'0','$remark','0','$user_name',NOW(),'$user_name',NOW())";
	mysql_query($sql);
	$order_id=mysql_insert_id ();
	if($add_id==""&&$user_id==0){
		//add_id为空,并且是游客状态下.地址表里记录的user_id就是点单的ID.并且创建人也是点单的ID.
		$sql="insert into zy_address(user_id,address,tel,phone,create_user,create_time,update_user,update_time) " .
				"values('$order_id','$address','$tel','$phone','$order_id',now(),'$order_id',now())";
		mysql_query($sql);
	}else if($add_id==""&&$user_id!=0){
		//add_id为空,并且是用户已登录状态下.新增一条用户地址.
		$sql="insert into zy_address(user_id,address,tel,phone,create_user,create_time,update_user,update_time) " .
				"values('$user_id','$address','$tel','$phone','$user_name',now(),'$user_name',now())";
		mysql_query($sql);
	}
	//插入数据到点单详细表中
	foreach($_SESSION['_cart']['food'] as $food){
		$sql="INSERT INTO `zy_item`(`order_id`,`food_id`,`food_name`,`food_price`,`count`,`subtotal`,`remark`,`is_delete`,`create_user`,`create_time`,`update_user`,`update_time`) 
				VALUES ('$order_id','".$food['food_id']."','".$food['food_name']."','".$food['price']."','".$food['quantity']."','".$food['price']*$food['quantity']."','','0','$user_name',now(),'$user_name',now());";
		mysql_query($sql);
	}
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<title>点一下,宁波最大的外卖订餐网站!</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="外卖,网上订餐,餐厅,美食" />
<meta name="description" content="查看附近的餐厅、美食信息" />
<meta http-equiv="X-UA-Compatible" content="chrome=IE6">
<script type="text/javascript" src="<?php echo SITE_ROOT;?>/js/jquery-1.8.0.js"></script>
<script type="text/javascript" src="<?php echo SITE_ROOT;?>/js/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div id="background"></div>
<div class="full-content-wrapper">
  <div class="cart-container">
    <div class="row cart-wrapper">
      <div class="span9 cart-header">
        <div class="cart-nav">
            <!-- 此处是为了不让字把云遮住 --> 
        </div>
      </div>
    <div class="span9 main-box bordered-box" style="margin-top:10px; text-align:center; line-height:40px; margin-bottom:30px; padding-top:30px;">
    &nbsp;您的美食篮子是空的&nbsp;- <a class="" href="<?php echo SITE_ROOT;?>">返回点一下</a>
    <div class="row">
      <div class="span3" style=" margin-left:270px;">
        <div class="cart-empty-bg"></div>
      </div>
          </div>
  </div>
  <?php include (dirname(__FILE__)."/include/footer.php"); ?>
  
